package com.ccp.dev.workflow.service.bpm.impl;

import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.SysUserService;
import com.ccp.dev.workflow.bpmconst.BpmConst;
import com.ccp.dev.workflow.dao.HistoryActivityDao;
import com.ccp.dev.workflow.model.BpmNodeUser;
import com.ccp.dev.workflow.model.bpm.CalcVars;
import com.ccp.dev.workflow.model.bpm.PreViewModel;
import com.ccp.dev.workflow.model.bpm.TaskExecutor;
import com.ccp.dev.workflow.service.bpm.IBpmNodeUserCalculation;
import net.sf.json.JSONObject;
import org.flowable.engine.impl.persistence.entity.HistoricActivityInstanceEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 根据节点用户设置为“与其他节点相同执行人”，计算执行人员。
 * 
 * @author fanruiqi
 */
@Component
public class BpmNodeUserCalculationSameNode implements IBpmNodeUserCalculation {
	@Resource
	SysUserService sysUserService;
	@Resource
	HistoryActivityDao historyActivityDao;

	@Override
	public List<SysUser> getExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		
		List<SysUser> users = new ArrayList<SysUser>();
		String cmpIds 	= bpmNodeUser.getCmpIds();
		JSONObject jsonObject = JSONObject.fromObject(cmpIds);
		String nodeId 	= jsonObject.getString("nodeId");

		String executionId= (String) vars.getVariable(BpmConst.EXECUTION_ID_);
		
		if(StringUtil.isEmpty(executionId)) {
            return users;
        }
		List<HistoricActivityInstanceEntity> hisList=  historyActivityDao.getByExecutionId(executionId, nodeId);
		
		if(BeanUtils.isEmpty(hisList)) {//如果找不到，然后对流程实例到找一次
			String procInstId= vars.getProcInstId();
			if(StringUtil.isEmpty(procInstId)) {
                return users;
            }
			hisList=  historyActivityDao.getByProcInstId(procInstId, nodeId);
		}
		
		if(BeanUtils.isEmpty(hisList)) {
            return users;
        }
		HistoricActivityInstanceEntity hiEnt = hisList.get(0);
		
		if(StringUtil.isNotEmpty(hiEnt.getAssignee())){
			SysUser user = sysUserService.getById(hiEnt.getAssignee());
			users.add(user);
		}
		
		
		return users;
	}

	@Override
	public String getTitle() {
		//return "与已执行节点相同执行人";
		return "与已执行节点相同执行人";
	}

	@Override
	public Set<TaskExecutor> getTaskExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		Set<TaskExecutor> uIdSet = new LinkedHashSet<TaskExecutor>();
		List<SysUser> sysUsers = this.getExecutor(bpmNodeUser, vars);
		if(BeanUtils.isNotEmpty(sysUsers)){
			for (SysUser sysUser : sysUsers) {
				if(BeanUtils.isNotEmpty(sysUser)){
					uIdSet.add(TaskExecutor.getTaskUser(sysUser.getUserId().toString(),sysUser.getFullName()));
				}
			}
		}
		
		return uIdSet;
	}

	@Override
	public boolean supportMockModel() {
		
		return false;
	}

	@Override
	public List<PreViewModel> getMockModel(BpmNodeUser bpmNodeUser) {
		
		return null;
	}
	
	@Override
	public boolean supportPreView() {
		return false;
	}

}
